<?php
class ome_ctl_admin_check extends desktop_controller{
    var $name = "货物校验";
    var $workground = "delivery_center";

    function index(){
        $deliveryObj  = &$this->app->model('delivery');
        $this->pagedata['num'] = $deliveryObj->countNoVerifyDelivery();
        $this->page("admin/delivery/process_check_index.html");
    }

    /**
     * 校验发货单是否可打印
     *
     */
    function check(){
    	$product_name_show_type = $this->app->getConf('ome.delivery.check_show_type');
    	empty($product_name_show_type) && $product_name_show_type = 'stock';
    	
    	$status_cfg = $this->app->getConf('ome.delivery.status.cfg');
        $autohide = array('autohide'=>3000);
        $this->begin('index.php?app=ome&ctl=admin_check');
        if (!$_POST['delivery']['logi_no']){
            $this->end(false, '请输入快递单号', '', $autohide);
        }
        $delivery     = $_POST['delivery'];
        $deliveryObj  = &$this->app->model('delivery');
        $dly          = $deliveryObj->dump(array('logi_no'=>$delivery['logi_no']));//,'*', array('delivery_items'=>array('*')));

        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_ids = $oBranch->getBranchByUser(true);
           if (!is_array($branch_ids) || !in_array($dly['branch_id'],$branch_ids))
           $this->end(false, $delivery['delivery_bn'].':发货单号不在您管辖的仓库范围内！', '', $autohide);
        }

        $doObj = app::get('ome')->model('delivery_order');
        $orders  = $doObj->dump(array('delivery_id'=>$dly['delivery_id']),'order_id');
        $order_id = $orders['order_id'];
        $oObj  = &$this->app->model('orders');
        $order_detail = $oObj->dump(array('order_id'=>$order_id),'pay_status');

        //增加部分退款，全额退款无法校验的约束
        if($order_detail['pay_status'] == 4){
        	$this->end(false, '订单已部分退款，无法校验。', '', $autohide);
        }
        if($order_detail['pay_status'] == 5){
            $this->end(false, '订单已全额退款，无法校验。', '', $autohide);
        }

        if (!$dly){
            $this->end(false, '无此快递单号', '', $autohide);
        }
        //判断发货单相应订单是否有问题
        if (!$this->checkOrderStatus($dly['delivery_id'], true, $msg)){
            $this->end(false, $msg, '', $autohide);
        }
        if ($dly['verify'] == 'true'){
            $this->end(false, '发货单已校验完成', '', $autohide);
        }
        if ($dly['status'] != 'progress'){
            $this->end(false, '此发货单不满足校验需求', '', $autohide);
        }
        if ($dly['pause'] == 'true'){
            $this->end(false, '此发货单已暂停', '', $autohide);
        }
        //增加配置打印配置后的判定修改
        if($status_cfg != ''){
			if($status_cfg['set']['stock'] == 1){
				if ($dly['stock_status'] == 'false'){//备货单是否打印
	                $this->end(false, '备货单未打印', '', $autohide);
	            }
			}

			if($status_cfg['set']['delie'] == 1){
				if ($dly['deliv_status'] == 'false'){//备货单是否打印
	                $this->end(false, '商品清单未打印', '', $autohide);
	            }
			}

			if ($dly['expre_status'] == 'false'){//快递单是否打印
	           $this->end(false, '快递单未打印', '', $autohide);
	        }

        }else{
	        if ($dly['stock_status'] == 'false'){//备货单是否打印
	            $this->end(false, '备货单未打印', '', $autohide);
	        }
	        if ($dly['deliv_status'] == 'false'){//商品清单是否打印
	            $this->end(false, '商品清单未打印', '', $autohide);
	        }
	        if ($dly['expre_status'] == 'false'){//快递单是否打印
	            $this->end(false, '快递单未打印', '', $autohide);
	        }
        }
        $pObj = &$this->app->model('products');
        $items = $deliveryObj->getItemsByDeliveryId($dly['delivery_id']);
        foreach ($items as $k => $i){
            $p = $pObj->dump($i['product_id'], 'barcode,spec_info');
            $count += $i['number'];
            $verify_num += $i['verify_num'];
            $items[$k]['barcode'] = trim($p['barcode']);
            $items[$k]['spec_info'] = trim($p['spec_info']);
            $items[$k]['bn'] = trim($items[$k]['bn']);
            if ($i['verify_num'] == $i['number']) {
                $items[$k]['nameColor'] =  '#eeeeee';
            } elseif ($i['verify_num'] > 0) {
                $items[$k]['nameColor'] ='red';
            } else {
                $items[$k]['nameColor'] = 'black';
            }
            $verify += $i['verify_num'];
        }

        if ($dly['is_bind'] == 'true'){
          $ids = $deliveryObj->getItemsByParentId($dly['delivery_id'], 'array', '*');
          $this->pagedata['ids'] = $ids;
        }

        //增加发货单校验显示前的扩展
        foreach(kernel::servicelist('ome.delivery') as $o){
            if(method_exists($o,'pre_check_display')){
                $o->pre_check_display($items);
            }
        }
        
        if($product_name_show_type == 'stock') {
            $this->toGoodsName($items);
        }

        $conf = &app::get('ome')->getConf('ome.delivery.check');
        $this->pagedata['normal'] = 0;
        $this->pagedata['conf'] = $conf;
        $this->pagedata['count'] = $count;
        $this->pagedata['number'] = $verify;
        $this->pagedata['items'] = $items;
        $this->pagedata['dly'] = $dly;
        $this->pagedata['verify_num'] = $verify_num;
        $this->pagedata['remain'] = $count - $verify_num;

        $checkType = $this->app->getConf('ome.delivery.check_type');
        if($checkType == 'all'){
            $this->page("admin/delivery/delivery_checkout2.html");
        }else{
            $this->page("admin/delivery/delivery_checkout.html");
        }
    }

    private function toGoodsName(& $items) {
        $goodsObj = &$this->app->model('goods');
        
        $bn_string = '';
        $len = count($items);
        for($i=0; $i<$len; $i++) {
            $goods = $goodsObj->dump(array('bn'=>$items[$i]['bn']));
            
            $items[$i]['product_name'] = $goods['name'];
        }
    }

    /**
     * 发货单内容校验
     *
     */
    function doCheck(){
        $autohide = array('autohide'=>2000);
        $this->begin('index.php?app=ome&ctl=admin_check&check');
        if (empty($_POST['delivery_id'])){
            $this->end(false, '发货单ID传入错误', '', $autohide);
        }
        if ($_POST['logi_no'] == ''){
            $this->end(false, '请扫描快递单号', '', $autohide);
        }

        foreach(kernel::servicelist('ome.delivery') as $o){
            if(method_exists($o,'pre_docheck')){
                $message = "";
                $result = $o->pre_docheck($_POST,$message);
                if(!$result){
                    $this->end(false, $message, '', $autohide);
                }
            }
        }


        $dly_id   = $_POST['delivery_id'];
        $count    = $_POST['count'];
        $number   = $_POST['number'];
        $logi_no  = $_POST['logi_no'];
        $this->checkOrderStatus($dly_id);//判断发货单相应订单是否有问题
        if ($count == 0 || $number == 0){
            $this->end(false, '对不起，校验提交的数据错误', '', $autohide);
        }
        $deliveryObj  = &$this->app->model('delivery');
        $dly          = $deliveryObj->dump($dly_id,'*', array('delivery_items'=>array('*')));
        $verify = $dly['verify'];
        if ($dly['logi_no'] != $logi_no){
            $this->end(false, '扫描的快递单号与系统中的快递单号不对应', '', $autohide);
        }
        $total = 0;
        foreach ($dly['delivery_items'] as $i){
            $total += $i['number'];
        }
        /*if ($number != $total){
            $this->end(false, '对不起，校验提交的数据与发货单数据不对应', '', $autohide);
        }*/
        $opObj        = &$this->app->model('operation_log');
        $dly_itemObj  = &app::get('ome')->model('delivery_items');

        if ($count === $number) {
            //对发货单详情进行校验完成处理
            if ($deliveryObj->verifyDelivery($dly)){
                //增加发货单校验把保存后的扩展
                foreach(kernel::servicelist('ome.delivery') as $o){
                    if(method_exists($o,'after_docheck')){
                        $data = $_POST;
                        $o->after_docheck($data);
                    }
                }
                $this->end(true, '发货单校验完成');
            }else {
                $this->end(false, '发货单校验未完成，请重新校验', 'index.php?app=ome&ctl=admin_check', $autohide);
            }
        } else {
            //保存部分校验结果
            $flag = $dly_itemObj->verifyItemsByDeliveryIdFromPost($dly_id);
            if ($flag){
                $opObj->write_log('delivery_check@ome', $dly_id, '发货单部分检验数据保存完成');
                $this->end(true, '发货单部分检验数据保存完成', '', $autohide);
            }else {
                $this->end(false, '发货单校验未完成，请重新校验', '', $autohide);
            }
        }
    }



    /**
     * 判断发货单号相关订单处理状态是否处于取消或异常
     *
     * @param bigint $dly_id
     * @return null
     */
    function checkOrderStatus($dly_id, $msg_flag=false, &$msg=NULL){
        if (!$dly_id) return false;
        $Objdly = &$this->app->model('delivery');
        $delivery = $Objdly->dump($dly_id);
        if (!$Objdly->existOrderStatus($dly_id, $delivery['is_bind'])){
            $msg = "发货单已无法操作，请到订单处理中心处理";
            if ($msg_flag == false){
                echo $msg;
                exit("<script>parent.MessageBox.error('发货单已无法操作，请到订单处理中心处理!');</script>");
            }else{
                return false;
            }
        }
        if (!$Objdly->existOrderPause($dly_id, $delivery['is_bind'])){
            $msg = "发货单相关订单已暂停，请到订单处理中心处理";
            if ($msg_flag == false){
                echo $msg;
                exit("<script>parent.MessageBox.error('发货单相关订单已暂停，请到订单处理中心处理!');</script>");
            }else{
                return false;
            }
        }
        return true;
    }
}
?>
